// index.js
var util = require('../../utils/util');

Page({
  data: {
    info: "",
    length: 10,
    key: "",
    appInstance: "",
    eventChannel: "",
    disabled: false,
    index: ""
  },
  onLoad(options) {
    var appInstance = getApp();
    this.data.appInstance = appInstance;
    this.data.eventChannel = this.getOpenerEventChannel();
    this.data.eventChannel.on('infoData', (data) => {
      var obj = data.data;
      var title = obj.title;
      this.data.key = obj.key;
      this.data.index = obj.index;
      if (title) {
        wx.setNavigationBarTitle({
          title: title,
        })
      }
      if (obj.length) {
        this.setData({
          length: obj.length
        })
      }
      if (obj.data) {
        this.setData({
          info: obj.data
        })
      }
    })
  },
  inputValueChange(event) {
    this.data.info = event.detail.value
  },
  updateOtherInfo() {

    if (this.data.disabled) {
      return;
    }
    this.data.disabled = true;
    var url = '/applet/updateRemark';
    var key = this.data.key;
    var data = {};
    //当前用户
    var index = this.data.index;
    if (util.isBlank(this.data.info)) {
      wx.showToast({
        title: '请输入有效字符',
        icon: 'error',
        duration: 2000
      })
      this.data.disabled = false;
      return;
    }

    if (key == "remark") {
      data.remark = this.data.info;
      var chat = wx.getStorageSync('chatList')[index];
      data.friendId = chat.friendId;
      data.userId = chat.userId;
      data.id = wx.getStorageSync('userInfo').id;
    } else {
      wx.showToast({
        title: '修改失败',
        icon: 'error',
        duration: 2000
      })
      this.data.disabled = false;
      return;
    }
    console.log(data)
    util.postRequest(url,
      data,
      (data) => {
        console.log(data)
        this.data.disabled = false;
        if (data.code == 2000) {
          if (key == "remark") {
            var chatList = wx.getStorageSync('chatList');
            if (chatList) {
              var content = chatList[index];
              content.remark = this.data.info
              wx.setStorageSync('chatList', chatList)
              this.data.eventChannel.emit('backShow', {
                data: content
              })
            }
          }
          wx.navigateBack();
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'error',
            duration: 2000
          })
        }
      },
      (error) => {
        this.data.disabled = false;
        wx.showToast({
          title: '修改失败',
          icon: 'error',
          duration: 2000
        })
      })
  },
  updateSelfInfo() {
    if (this.data.disabled) {
      return;
    }
    this.data.disabled = true;
    var url = '/applet/updateUserInfo';
    var key = this.data.key;
    var id = wx.getStorageSync('userInfo').id
    var data = {};
    data.id = id;
    if (util.isBlank(this.data.info)) {
      wx.showToast({
        title: '请输入有效字符',
        icon: 'error',
        duration: 2000
      })
      this.data.disabled = false;
      return;
    }
    if (key == "username") {
      data.username = this.data.info;
    } else if (key == "sign") {
      data.sign = this.data.info;
    } else if (key == "phoneNumber") {
      data.phoneNumber = this.data.info;
    } else {
      wx.showToast({
        title: '修改失败',
        icon: 'error',
        duration: 2000
      })
      this.data.disabled = false;
      return;
    }
    util.postRequest(url,
      data,
      (data) => {
        this.data.disabled = false;
        if (data.code == 2000) {

          var userInfo = wx.getStorageSync('userInfo');
          if (key == "username") {
            userInfo.username = this.data.info;
          } else if (key == "sign") {
            userInfo.sign = this.data.info;
          } else if (key == "phoneNumber") {
            userInfo.phoneNumber = this.data.info;
          }
          wx.setStorageSync('userInfo', userInfo)
          this.data.eventChannel.emit('backShow', {
            data: userInfo
          })

          wx.navigateBack();
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'error',
            duration: 2000
          })
        }
      },
      (error) => {
        this.data.disabled = false;
        wx.showToast({
          title: '修改失败',
          icon: 'error',
          duration: 2000
        })
      })
  },
  save() {
    console.log(this.data)

    if (this.data.index >= 0) {
      this.updateOtherInfo();
    } else {
      this.updateSelfInfo();
    }



  },
  cancel() {
    wx.navigateBack();
  }
})